/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package consys.social.core.service;

import consys.social.core.NotAuthorizedException;
import consys.social.core.bo.Entity;
import consys.social.core.bo.WallPost;
import consys.social.core.dao.LikeDao;
import java.io.IOException;
import java.util.List;

/**
 *
 * @author petan
 */
public class LikeServiceImpl implements LikeService {

    private LikeDao likeDao;
    private FollowService followService;

    public void setLikeDao(LikeDao likeDao) {
        this.likeDao = likeDao;
    }

    public void setFollowService(FollowService followService) {
        this.followService = followService;
    }

    @Override
    public void like(Entity who, WallPost what) throws IOException, NotAuthorizedException {
        Entity source = AuthenticatedUser.getUser();
        if (who == null) {
            throw new IllegalArgumentException("Entity who wants to like is null.");
        }
        if (!who.equals(source)) {
            throw new NotAuthorizedException("User is performing an action for an other user");
        }
        if (what == null) {
            throw new IllegalArgumentException("Post you want to like is null");
        }
        if (!followService.isFollowing(who, what.getAddedBy())) {
            throw new NotAuthorizedException("You can only like posts from people you follow");
        }
        likeDao.like(who, what);
    }

    @Override
    public void unlike(Entity who, WallPost what) throws IOException, NotAuthorizedException {
        Entity source = AuthenticatedUser.getUser();
        if (who == null) {
            throw new IllegalArgumentException("Entity who wants to unlike is null.");
        }
        if (!who.equals(source)) {
            throw new NotAuthorizedException("User is performing an action for an other user");
        }
        if (what == null) {
            throw new IllegalArgumentException("Post you want to unlike is null");
        }
        likeDao.unlike(who, what);
    }

    @Override
    public List<Entity> getLikes(WallPost post) throws IOException {
        if (post == null) {
            throw new IllegalArgumentException("Post you want to get likes is null.");
        }
        return likeDao.getLikes(post);
    }
}